/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controller;

import common.Constant;
import dao.UserDAO;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.UserModel;

/**
 *
 * @author ThanhToan
 */
public class UserController extends HttpServlet {
    private String URL = "";

    private void mapParameterToModel(HttpServletRequest req
			,HttpServletResponse res
				,UserModel userModel) {
        if (req.getParameter("txtUsername") != null) {
			userModel.setUsername(req.getParameter("txtUsername").trim()); }
        if (req.getParameter("txtPassword") != null) {
			userModel.setPassword(req.getParameter("txtPassword").trim()); }
        if (req.getParameter("txtFirstname") != null) {
			userModel.setFirstname(req.getParameter("txtFirstname").trim()); }
        if (req.getParameter("txtLastname") != null) {
			userModel.setLastname(req.getParameter("txtLastname").trim()); }
        if (req.getParameter("txtEmail") != null) {
			userModel.setEmail(req.getParameter("txtEmail").trim()); }
        if (req.getParameter("txtPhone") != null) {
			userModel.setPhone(req.getParameter("txtPhone").trim()); }
        if (req.getParameter("txtAddress") != null) {
			userModel.setAddress(req.getParameter("txtAddress").trim()); }
        userModel.setDay(req.getParameter("cboDay").trim());
        userModel.setMonth(req.getParameter("cboMonth").trim());
        userModel.setYear(req.getParameter("cboYear").trim());
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
        doPost(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
        if(request.getParameter("register") != null)
	{
            register(request, response);
	}
        if(request.getParameter("profile") != null)
	{
            updateProfile(request, response);
	}
        if(request.getParameter("changePass") != null)
	{
            changePass(request, response);
	}
    }

    private void register(HttpServletRequest request
			,HttpServletResponse response)throws ServletException, IOException {
        UserModel userModel = new UserModel();
        mapParameterToModel(request, response, userModel);
        if(userModel.validate()){
            if(UserDAO.insertUser(userModel)){
                URL = "registerSuccess.jsp";
            }
        }
        else{ //validate err
            URL = "register.jsp?err='Validate error'";
        }
        RequestDispatcher rd = request.getRequestDispatcher(URL);
        request.setAttribute(Constant.USER_INFO, userModel);
        rd.forward(request, response);
    }

    private void updateProfile(HttpServletRequest request
			,HttpServletResponse response)throws ServletException, IOException {
        UserModel userModel = new UserModel();
        mapParameterToModel(request, response, userModel);
        if(userModel.validate()){
            if(UserDAO.updateUser(userModel)){
                URL = "profile.jsp?update='success'";
            }
        }
        else{
            URL = "profile.jsp?err='Validate error'";
        }
        RequestDispatcher rd = request.getRequestDispatcher(URL);
        request.setAttribute(Constant.USER_INFO, userModel);
        rd.forward(request, response);
    }

    private void changePass(HttpServletRequest request
			,HttpServletResponse response)throws ServletException, IOException {
        String userID = request.getParameter("txtUserID");
        String pass = request.getParameter("txtNPass");
        if(UserDAO.changePass(pass, userID)){
            URL = "logout.jsp";
        }
        RequestDispatcher rd = request.getRequestDispatcher(URL);
        rd.forward(request, response);
    }

    public String getServletInfo() {
        return "Short description";
    }
}
